<template>
    <!-- 评论 -->
    <div class="comment-container">
        <textarea  placeholder="请输入要发表的内容，最多120字" maxlength="120" v-model="comment"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <!-- 评论列表 -->
        <div class="comment-list" v-for="(item, i) in commentList" :key="i">
            <div class="comment-title">
                第{{ i + 1 }}楼&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;&nbsp;发表时间：{{ item.add_time }}
            </div>
            <div class="comment-content">
                {{ item.content === 'undefined' ? '该用户很懒，没有写内容' : item.content }}
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import { Toast }  from 'mint-ui'
    export default {
        name: "Comment",
        data() {
            return {
                commentList: [],
                pageIndex: 1,
                comment: ''
            }
        },
        created() {
            this.getCommentList()
        },
        methods: {
            getCommentList() {
                this.$http.get('subcomponents/Comment.json?id=' + this.id + '&pageIndex=' + this.pageIndex).then( result => {
                    if (result.body.status === 0) {
                        // 拼接数据实现加载更多效果
                        this.commentList = this.commentList.concat(result.body.message);
                    } else {
                       Toast('获取评论失败');
                    }
                })
            },
            getMore() {  // 加载更多
                this.pageIndex ++;
                this.getCommentList();
            },
            postComment() {
                // 非空校验
                if (this.comment.trim().length === 0) {
                    return Toast('评论为空，不能提交');
                }
                // 提交评论
                /* // 本地不支持post提交
                this.$http.post('subcomponents/PostComment.json/' + this.$route.params.id, {
                    comment: this.comment.trim()
                }).then( result => {
                    if (result.body.status === 0) {
                        Toast(result.body.message);
                    } else {
                        Toast('提交评论失败');
                    }
                })
*/
                // 因为是本地数据，不能使用post提交，于是使用get来模拟提交
                this.$http.get('subcomponents/PostComment.json?id=' + this.$route.params.id).then( result => {
                    if (result.body.status === 0) {
                        Toast(result.body.message);
                        // 拼接评论内容
                        var commentObj = {
                            user_name: '匿名用户',
                            add_time: Date.now(),
                            content: this.comment
                        };
                        // 评论置顶
                        this.commentList.unshift(commentObj);
                        // 清空评论
                        this.comment = '';
                    } else {
                        Toast('提交评论失败');
                    }
                })

            }
        },
        props: ['id']
    }
</script>

<style lang="scss" scoped>
.comment-container {
    textarea {
        margin-bottom: 3px;
        font-size: 14px;
    }
    .comment-list{

        .comment-title {
            font-size: 13px;
            background-color: #eeeeee;
            padding-left: 5px;
            margin-bottom: 2px;
        }
        .comment-content {
            font-size: 12px;
            text-indent: 2em;
        }
    }
}
</style>
